//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import complaintTag from '../comm/complaintTag.vue';
import compContent from '../comm/compContent.vue';
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {complaintTag,compContent},
  data() {
  return {
    id:'',
    tagList:[],
    relationForm:{
      imgs:[],
      mobile:'',
      email:'',
      content:'',
      problem_ids:[]
    }
  };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    tagClick(data) {
      let {index} = data;
      this.tagList[index].checked = !this.tagList[index].checked;
    },
    complainMe() {
      this.$router.push(this.fun.getUrl("complaintList"));
    },
    initData() {
      this.relationForm.imgs = [];
      this.relationForm.mobile = '';
      this.relationForm.content = '';
      this.relationForm.email = '';
      this.relationForm.problem_ids = '';
    },
    problemData() {
      $http.post("plugin.help-center.frontend.controllers.proposal.index.problem-list" ,{}).then(res => {
        if(res.result == 1) {
          let data = res.data;
          data.map((item,index) => {
            this.$set(item,'checked',false);
          })
         
          this.tagList = data;
          console.log(this.tagList);
        } else {
          this.$dialog.alert({ message: response.msg });
        }
      });
    },
    problemDetails() {
      $http.post("plugin.help-center.frontend.controllers.proposal.index.details" ,{id:this.id}).then(res => {
        if(res.result == 1) {
          let data = res.data;
          this.relationForm.imgs = data.imgs;
          this.relationForm.content = data.content;
          this.relationForm.email = data.email;
          this.relationForm.mobile = data.mobile;
          let problems = [];
          data.problems.map((item,index) => {
            problems.push({
             id:item.problem?.id,
             name:item.problem?.name,
             checked:true
            });
          })
          this.tagList = problems;
          console.log(this.tagList);
        } else {
          this.$dialog.alert({ message: response.msg });
        }
      });
    },
    relationBtn() {
      let  problemArr = this.tagList.filter((item) => item.checked);
      this.relationForm.problem_ids = problemArr.map((item,index) => item.id);
      if (this.relationForm.problem_ids.length <= 0) {
        this.$toast("请选择问题类型");
        return;
      }
      if (this.fun.isTextEmpty(this.relationForm.content)) {
        this.$toast("请输入问题信息");
        return;
      }
      if (this.fun.isTextEmpty(this.relationForm.mobile)) {
        this.$toast("请输入手机号");
        return;
      }
      $http.post("plugin.help-center.frontend.controllers.proposal.index", {form:this.relationForm}, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            if (window.history.length <= 1) {
              this.$router.push(this.fun.getUrl("home", {}));
            } else {
              this.$router.go(-1);
            }
            this.$toast(response.msg)
          } else {
            
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
      console.log(this.relationForm);
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
  
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
  
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    this.initData();
    if(this.$route.query.id) {
      this.id = this.$route.query.id;
      this.problemDetails();
    } else {
      this.problemData();
    }
    
    
  }, //如果页面有keep-alive缓存功能，这个函数会触发
  }